home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
039a
/
d3d.zip
/
GRPACK1.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-30
|
8KB
|
334 lines
/* GRPACK1: A graphics package, using Turbo C
graphics functions, available in
Version 1.5.
Memory model: Huge (for ALL modules involved)
For: Enhanced Graphics Adapter (EGA, 640 x 350)
Color Graphics Adapter (CGA, 640 x 200)
Hercules Graphics Adapter (HGA, 720 x 348)
*/
#include <stdio.h>
#include <dos.h>
#include <alloc.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#include <graphics.h>
union REGS regs;
int in_textmode=1, X__max, Y__max, drawmode=1, adaptype;
FILE *fplot=NULL;
float x_max=10.0, y_max=7.0, horfact, vertfact;
static int X1, Y1, foregroundcol, backgroundcol,
g_driver=9, g_mode=2;
static long int startaddress;
void to_text(void);
void textXY(int X, int Y, char *str);
int IX(float x) { return (int)(x*horfact+0.5); }
int IY(float y) { return Y__max - (int)(y*vertfact+0.5); }
static void error(char *str)
/* Display a message and terminate program execution */
{
if(!in_textmode)
to_text();
printf("%s\n",str);
exit(1);
}
void clearpage(void) /* clear the screen */
{
clearviewport();
}
int iscolor(void) /* Find out which adapter is used */
{
/* detectgraph(&g_driver, &g_mode);*/
printf("found %d %d\n",g_driver, g_mode);
if (g_mode == MCGAHI) g_mode = MCGAMED;
if (g_mode == ATT400HI) g_mode = ATT400MED;
if (g_mode == VGAHI) g_mode = VGAHI;
initgraph(&g_driver,&g_mode,""); /* Set up screen size */
return g_driver == CGA || g_driver == MCGA ||
g_driver == ATT400 ?
1 /* 640 x 200 */ :
g_driver == EGA || g_driver == EGA64 ||
g_driver == EGAMONO || g_driver == VGA ?
2 /* 640 x 480 */ :
g_driver == HERCMONO ? 0 /* 720 x 348 */ : -1;
}
static void setgrcon(int adaptype)
/* Set graphics constants */
{
startaddress = (adaptype == 2 ? 0xA0000000 : 0xB8000000);
if (adaptype == 2) {
X__max = 639; Y__max = 479; /* VGA */
}
else if(adaptype == 1) {
X__max = 639; Y__max = 199; /* CGA */
}
else {
X__max = 719; Y__max = 347; /* HGA */
}
}
static int grbrfun(void)
/* Used by ctrlbrk, to specify what to do with a console
break */
{
to_text(); /* Go to text mode first! */
return 0;
}
void initgr(void) /* Initialize graphics */
{
static int again=0;
int errcode;
if(again)
setgraphmode(g_mode);
else {
adaptype = iscolor(); /* detect driver and mode */
if(adaptype < 0)
error("Wrong display adapter");
ctrlbrk(grbrfun); /* Set break trap, Turbo C */
initgraph(&g_driver, &g_mode, "");
errcode = graphresult();
if(errcode < 0) {
printf("Graphics error code: %d", errcode);
exit(1);
}
again = 1;
setgrcon(adaptype);
}
horfact = X__max/x_max;
vertfact = Y__max/y_max;
if(adaptype == 0){ /* HCG */
setvisualpage(1);
setactivepage(1);
}
in_textmode = 0;
foregroundcol = getcolor();
backgroundcol = getbkcolor();
}
static int txtbrfun(void)
{
return 0;
}
void to_text(void)
/* Revert to text mode */
{
if(!in_textmode)
restorecrtmode();
in_textmode = 1;
ctrlbrk(txtbrfun);
/* Restore default break interrupt handler */
}
void endgr(void)
/* Wait until any key is hit and revert to text mode */
{
getch();
to_text();
}
void dot(int X, int Y) /* Light or darken a pixel */
{
putpixel(X, Y,
(drawmode == 0 ? (getpixel(X, Y) == foregroundcol
? backgroundcol : foregroundcol) :
(drawmode == 1 ? foregroundcol : backgroundcol)));
/* drawmode = 1: draw positively */
/* drawmode = -1: draw negatively */
/* drawmode = 0: toggle */
}
void checkbreak(void)
{
char ch;
if(kbhit()) {
ch = getch();
kbhit();
ungetch(ch);
}
}
void draw_line(int X1, int Y1, int X2, int Y2)
/* Draw the line segment from (X1,Y1) to (X2,Y2);
X1, Y1, X2, Y2 are pixel coordinates */
{
if(drawmode == 1)
line(X1, Y1, X2, Y2);
/* 'drawmode' values 0 and -1 are not implemented for
this function. See also dot()
*/
}
static void fatal(void)
/* Draw a diagonal, then wait until a key is pressed, and
finally revert to text mode
*/
{
draw_line(0,Y__max, X__max, 0);
endgr();
}
static void check(int X, int Y)
/* If point (X,Y) lies outside the screen boundaries, then
call fatal, display the incorrect coordinates, and stop
*/
{
if(X < 0 || X > X__max || Y < 0 || Y > Y__max) {
fatal();
printf("Point outside screen (X and Y are pixel coordinates):\n");
printf("X = %d Y = %d\n", X, Y);
printf("x = %10.3f y = %10.3f\n",X/horfact,(Y__max-Y)/vertfact);
printf("X__max = %d Y__max = %d x_max = %f y_max = %f\n",
X__max, Y__max, x_max, y_max);
printf("horfact = %f vertfact = %f\n", horfact, vertfact);
exit(1);
}
}
void move(float x, float y)
/* Move the current point to (X, Y)
X and Y are screen coordinates */
{
int XX, YY;
XX = IX(x);
YY = IY(y);
check(XX, YY);
if(fplot != NULL && (XX != X1 || YY != Y1))
fprintf(fplot, "%6.3f %6.3f 0\n", x, y);
X1 = XX;
Y1 = YY;
}
void draw(float x, float y)
/* Draw a line segment from the current point to (x, y) */
{
int X2, Y2;
X2 = IX(x);
Y2 = IY(y);
check(X2, Y2);
draw_line(X1, Y1, X2, Y2);
if(fplot != NULL)
fprintf(fplot, "%6.3f %6.3f 1\n", x, y);
X1 = X2;
Y1 = Y2;
}
int pixlit(int X, int Y)
/* Inquire if pixel (X, Y) is lit */
{
return(getpixel(X, Y) == foregroundcol);
}
void printgr(int Xlo, int Xhi, int Ylo, int Yhi)
/* Print contents of rectangle on matrix printer */
{
}
void setprdim(void)
/* This function sets x_max and y_max such that graphics
results will eventually be printed with correct
dimensions, both horizontally and vertically */
{
extern float x_max, y_max;
extern int X__max, Y__max;
setgrcon(iscolor());
x_max = (X__max + 1)/120.0;
y_max = (Y__max + 1) /72.0;
}
void textXY(int X, int Y, char *str)
/* Display string str in graphics mode, starting at (X, Y) */
{
int height, width;
height = textheight(str);
width = textwidth(str);
setviewport(X, Y, X+width, Y+height, 0);
clearviewport(); /* To erase old text, if any */
outtext(str);
setviewport(0, 0, X__max, Y__max, 0);
}
void text(char *str)
/* Display string str, starting at the current point (X1, Y1). */
{
textXY(X1, Y1, str);
X1 += strlen(str)*8;
check(X1, Y1);
}
void imove(int X, int Y)
/* Let (X, Y) be the new current point */
{
check(X, Y);
X1 = X;
Y1 = Y;
}
void idraw(int X, int Y)
/* Draw a line segment from the current poitnt to (X, Y) */
{
check(X, Y);
draw_line(X1, Y1, X, Y);
X1 = X;
Y1 = Y;
}
void wrscr(int row, int col, char *str) /* in textmode */
/* Writes characters into video display memory while in text mode
row = 0, 1, 2, ... ; col = 0, 1, 2, ..., 79 */
{
gotoxy(col+1, row+1);
cputs(str);
}
void clearscr(void)
/* Clear text mode screen (for graphics, use clearpage) */
{
clrscr();
}
void settxtcursor(int row, int col)
{
gotoxy(col+1, row+1);
}
void far *far _graphgetmem(unsigned size)
{
char *p;
p = farmalloc((long)size);
if(p == NULL)
error("Mem. space in _graphgetmem");
return p;
}
void far _graphfreemem(void far *ptr, unsigned size)
{
farfree(ptr);
}